package fem2.gui.pm;

import inf.vtk.VTKConvert;
import inf.vtk.pm.ActorManager;
import inf.vtk.pm.CurvesPipeline;
import inf.vtk.pm.IDLabelsPipeline;
import inf.vtk.pm.Pipeline;
import inf.vtk.pm.PipelineFactory;
import inf.vtk.pm.SolidElementsPipeline;
import inf.vtk.pm.SymbolsPipeline;

import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import vtk.vtkActor;
import vtk.vtkProperty;

public class HofemPipelineFactory implements PipelineFactory {

	private static final double[] HUE_RANGE = { 0.5, 1.11 };
	public static final String PL_SYMBOLS = "Symbols";
	public static final String PL_EDGES = "Edges";
	public static final String PL_ID_LABELS = "ID Labels";
	public static final String PL_FACES = "Faces";
	public static final String PL_LINE_PLOT = "Line plot";

	private static CurvesPipeline createEdgesPipeline() {
		CurvesPipeline p = new CurvesPipeline();
		ActorManager am = p.getActorManager(CurvesPipeline.ACTOR_DISPLACED_CURVES);
		vtkActor a = am.getCastedActor();

		a.GetMapper().ScalarVisibilityOff();
		a.GetProperty().SetColor(VTKConvert.convert(Color.BLACK));
		am.setVisible(false);
		return p;
	}

	private static Pipeline createFacesPipeline() {
		SolidElementsPipeline p = new SolidElementsPipeline();

		for (String n : p.getActorNames()) {
			p.getActorManager(n).setVisible(false);
		}

		p.getActorManager(SolidElementsPipeline.ACTOR_ELEMENTS_RC).setVisible(true);
		p.setHueRange(HUE_RANGE);

		p.getActorManager(SolidElementsPipeline.ACTOR_OUTLINES_RC).setVisible(true);

		vtkProperty ap = p.getActorManager(SolidElementsPipeline.ACTOR_OUTLINES_RC)
				.getCastedActor().GetProperty();
		ap.SetColor(0.6, 0.2, 0.2);
		ap.SetDiffuse(1);
		ap.SetAmbient(0);

		return p;
	}

	private static IDLabelsPipeline createIdLabelsPipeline() {
		IDLabelsPipeline p = new IDLabelsPipeline();

		for (String n : p.getActorNames()) {
			p.getActorManager(n).setVisible(false);
		}
		return p;
	}

	private static LinePlotPipeline createLinePlotPipeline() {
		LinePlotPipeline p = new LinePlotPipeline();

		for (String n : p.getActorNames()) {
			p.getActorManager(n).setVisible(false);
		}
		p.setHueRange(HUE_RANGE);
		return p;
	}

	private static SymbolsPipeline createSymbolsPipeline() {
		SymbolsPipeline p = new SymbolsPipeline();

		p.getActorManager(SymbolsPipeline.ACTOR_DISTRIBUTED_LOAD_SYMBOLS).setVisible(false);
		p.getActorManager(SymbolsPipeline.ACTOR_POINT_FORCE_SYMBOLS).setVisible(false);
		p.getActorManager(SymbolsPipeline.ACTOR_POINT_MOMENT_SYMBOLS).setVisible(false);
		return p;
	}

	private Map<String, Pipeline> pipelines_ = new HashMap<String, Pipeline>();

	public HofemPipelineFactory() {
		// pipelines_.put(PL_ID_LABELS, createIdLabelsPipeline()); // hbui: no
		// adapter
		// found
		pipelines_.put(PL_EDGES, createEdgesPipeline()); // hbui: no adapter
		// found for
		// CurvesPipeline
		pipelines_.put(PL_SYMBOLS, createSymbolsPipeline());
		// pipelines_.put(PL_LINE_PLOT, createLinePlotPipeline());
		pipelines_.put(PL_FACES, createFacesPipeline());
	}

	@Override
	public Pipeline getPipeline(String name) {
		return pipelines_.get(name);
	}

	@Override
	public Set<String> getPipelineNames() {
		return pipelines_.keySet();
	}
}
